/**  
* @Title: EhcacheAction.java
* @Package com.towerswatson.controller.ehcache
* @Description: TODO(用一句话描述该文件做什么)
* @author dingkunjie  
* @date 2016年2月17日 下午5:20:34
* @version V1.0  
*/ 
package com.baili.controller.ehcache;

import java.util.HashMap;
import java.util.Map;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baili.util.EhcacheUtil;


/**  
 * @Title: EhcacheAction.java
 * @Package com.towerswatson.controller.ehcache
 * @Description: 测试ehcache缓存
 * @author iceX  
 * @date 2016年2月17日 下午5:20:34
 * @version V1.0  
 */
@Controller
@RequestMapping("/ehcache")
public class EhcacheAction { 

	@Autowired
	private CacheManager cacheManager;
	/**
	* @Title: setParamsToEhcache
	* @Description: 向ehcache中存入值
	* @param @return String
	* @return null  
	* @throws null
	* @author 丁坤杰
	 * @throws Exception 
	*/
	@RequestMapping("/setParamsToEhcache")
	public String setParamsToEhcache() throws Exception {
		
		
		EhcacheUtil ehcache = new EhcacheUtil();
		ehcache.addToCache("ddd", "ddd");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("key1", "aaa");
		map.put("key2", "bbb");
		
		ehcache.addToCache("map", map);
		
		return "success";
	}
	
	/**
	 * @Title: setParamsToEhcache1
	 * @Description: 向ehcache中存入值
	 * @param @return String
	 * @return null  
	 * @throws null
	 * @author 丁坤杰
	 * @throws Exception 
	 */
	@RequestMapping("/setParamsToEhcache1")
	public String setParamsToEhcache1() throws Exception {
		
		Cache cache = cacheManager.getCache("ehcacheName");
		
		Element element = new Element("ddd", "ddd");
		cache.put(element);
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("key1", "aaa");
		map.put("key2", "bbb");
		
		Element element1 = new Element("map", map);
		cache.put(element1);
		
		return "success";
	}
	
	/**
	 * @Title: getParamsToEhcache
	 * @Description: 从ehcache中取出值
	 * @param @return String
	 * @return null  
	 * @throws null
	 * @author 丁坤杰
	 * @throws Exception 
	 */
	@RequestMapping("/getParamsToEhcache")
	public String getParamsToEhcache() throws Exception {
		
		
		EhcacheUtil ehcache = new EhcacheUtil();
		Object str = ehcache.getCacheElement("ddd");
		System.out.println(str);
		
		@SuppressWarnings("unchecked")
		Map<String,Object> map = (Map<String, Object>) ehcache.getCacheElement("map");
		System.out.println(map.get("key1"));
		System.out.println(map.get("key2"));
		
		return "success";
	}
	
	/**
	 * @Title: getParamsToEhcache1
	 * @Description: 从ehcache中取出值
	 * @param @return String
	 * @return null  
	 * @throws null
	 * @author 丁坤杰
	 * @throws Exception 
	 */
	@RequestMapping("/getParamsToEhcache1")
	public String getParamsToEhcache1() throws Exception {
		Cache cache = cacheManager.getCache("ehcacheName");
		Element e = cache.get("ddd");
		
		@SuppressWarnings("deprecation")
		Object str = e.getValue();
		System.out.println(str);
		
		Element e2 = cache.get("map");
		@SuppressWarnings({ "unchecked", "deprecation" })
		Map<String,Object> map = (Map<String, Object>) e2.getValue();
		System.out.println(map.get("key1"));
		System.out.println(map.get("key2"));
		
		return "success";
	}
}
